// pages/admin/user/index.js
import {
  request
} from '../../request'
const app = getApp();
Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    sysflag: false,
    memberMan: false,
    op: false,
    te: false,
    hy: false,
    scroll_height:100,
    userManager: [{
        icon: 'friendfamous',
        color: 'blue',
        name: '用户管理',
        url: '/pages/sys/user/list/index'
      },
      {
        icon: 'friend',
        color: 'blue',
        name: '角色管理',
        url: '/pages/sys/role/list/index'
      },
      {
        icon: 'list',
        color: 'blue',
        name: '部门管理',
        url: '/pages/sys/dept/list/index'
      }
    ],
    member: [{
        icon: 'profile',
        color: 'blue',
        name: '会员管理',
        url: '/pages/member/people/list/index'
      },
      {
        icon: 'card',
        color: 'blue',
        name: '会员卡管理',
        url: '/pages/member/card/list/index'
      },
      {
        icon: 'form',
        color: 'blue',
        name: '会员卡使用记录',
        url: '/pages/member/usage/list/index'
      }
    ],
    operation: [{
        icon: 'cart',
        color: 'blue',
        name: '商品管理',
        url: '/pages/opreation/commodity/list/index'
      },
      {
        icon: 'btn',
        color: 'blue',
        name: '租柜管理',
        url: '/pages/opreation/cabinet/list/index'
      },
      {
        icon: 'link',
        color: 'blue',
        name: '学员分配',
        url: '/pages/opreation/studentAssignment/list/index'
      }
    ],
    teacher: [{
      icon: 'peoplelist',
      color: 'blue',
      name: '我的学生',
      url: '/pages/teacher/myStudets/list/index'
    }],
    hylist: [{
      icon: 'shop',
      color: 'blue',
      name: '商品',
      url: '/pages/member/goodList/list/index'
    },
    {
      icon: 'expressman',
      color: 'blue',
      name: '我的私教',
      url: '/pages/member/myTeacher/list/index'
    }],
    gridCol: 3,
    skin: false
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      this.setPre();
      let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
      let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
      let ratio = 750 / windowWidth;
      this.setData({
        scroll_height: (windowHeight - 100) * ratio
      })
    }
  },
  pageLifetimes: {
    show: function () {
      var that = this;
      request('/getInfo', "GET").then(resopnse => {
        wx.setStorageSync('roles', resopnse.roles);
        wx.setStorageSync('userId', resopnse.user.userId);
        that.setPre();
      });
    }
  },
  methods: {
    setPre: function () {
      if (this.checkRoles('manager')) {
        this.setData({
          sysflag: true,
        })
      }
      if (this.checkRoles('manager,reception')) {
        this.setData({
          memberMan: true,
        })
      }
      if (this.checkRoles('manager,reception')) {
        this.setData({
          op: true,
        })
      }
      if (this.checkRoles('teacher')) {
        this.setData({
          te: true,
        })
      }
      if (this.checkRoles('member')) {
        this.setData({
          hy: true,
        })
      }
    },
    checkRoles: function (role) {
      var flag = false;
      var roles = wx.getStorageSync("roles");
      console.log(roles)
      var roleList = role.split(",");
      roleList.forEach(r => {
        if (roles.indexOf(r) >= 0) {
          flag = true;
        }
      })
      roleList.forEach(r => {
        if (roles.indexOf('admin') >= 0) {
          flag = true;
        }
      })
      console.log(flag)
      return flag;
    }
  }
})